package com.yiren.dbaa.module.help_center.view.activity;

import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;
import com.yiren.dbaa.module.help_center.view.fragment.ToolkitFragment;
import com.yiren.dbaa.module.help_center.view.fragment.UpdateFragment;
import com.yiren.dbaa.module.help_center.view.fragment.WebViewFragment;
import com.yiren.dbaa.module.help_center.view.fragment.DeviceInfoFragment;
import com.yiren.dbaa.module.practice_set.site.view.adapter.CarPracticeSetNameAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/7/25 13:53
 * @description: 售后服务首页
 */
public class AfterServiceActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.lv_name)
    ListView lvName;
    @BindView(R.id.fl_content)
    FrameLayout flContent;

    private DeviceInfoFragment deviceInfoFragment;
    private WebViewFragment webviewFragment;
    private WebViewFragment webViewFragment1;
    private ToolkitFragment toolkitFragment;
    private UpdateFragment updateFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_after_service);
        ButterKnife.bind(this);
        initDatas();//初始化数据
        selectTab(0);//默认选中第一个Tab
    }

    private void initDatas() {
        tvTitle.setText("帮助中心");
        List<String> setNameList = new ArrayList<>();
        setNameList.add("设备信息");
        setNameList.add("联系我们");
        setNameList.add("自助服务");
        setNameList.add("工具包下载");
        setNameList.add("系统更新");
        CarPracticeSetNameAdapter carPracticeSetNameAdapter = new CarPracticeSetNameAdapter(this, setNameList);
        lvName.setAdapter(carPracticeSetNameAdapter);
        lvName.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                carPracticeSetNameAdapter.changeSelected(position);
                selectTab(position);
            }
        });
    }

    @OnClick({R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
        }
    }

    //进行选中Tab的处理
    private void selectTab(int i) {
        //获取FragmentManager对象
        FragmentManager manager = getSupportFragmentManager();
        //获取FragmentTransaction对象
        FragmentTransaction transaction = manager.beginTransaction();
        //先隐藏所有的Fragment
        hideFragments(transaction);
        switch (i) {
            //当选中点击的是第一页的Tab时
            case 0:
                //如果第一页对应的Fragment没有实例化，则进行实例化，并显示出来
                if (deviceInfoFragment == null) {
                    deviceInfoFragment = new DeviceInfoFragment();
                    transaction.add(R.id.fl_content, deviceInfoFragment);
                } else {
                    //如果第一页对应的Fragment已经实例化，则直接显示出来
                    transaction.show(deviceInfoFragment);
                }
                break;
            case 1:
                if (webviewFragment == null) {
                    webviewFragment = new WebViewFragment();
                    transaction.add(R.id.fl_content, webviewFragment);
                } else {
                    transaction.show(webviewFragment);
                }
                break;
            case 2:
                if (webViewFragment1 == null) {
                    webViewFragment1 = new WebViewFragment();
                    transaction.add(R.id.fl_content, webViewFragment1);
                } else {
                    transaction.show(webViewFragment1);
                }
                break;
            case 3:
                if (toolkitFragment == null) {
                    toolkitFragment = new ToolkitFragment();
                    transaction.add(R.id.fl_content, toolkitFragment);
                } else {
                    transaction.show(toolkitFragment);
                }
                break;
            case 4:
                if (updateFragment == null) {
                    updateFragment = new UpdateFragment();
                    transaction.add(R.id.fl_content, updateFragment);
                } else {
                    transaction.show(updateFragment);
                }
                break;
        }
        //不要忘记提交事务
        transaction.commit();
    }

    //将四个的Fragment隐藏
    private void hideFragments(FragmentTransaction transaction) {
        if (deviceInfoFragment != null) {
            transaction.hide(deviceInfoFragment);
        }
        if (webviewFragment != null) {
            transaction.hide(webviewFragment);
        }
        if (webViewFragment1 != null) {
            transaction.hide(webViewFragment1);
        }
        if (toolkitFragment != null) {
            transaction.hide(toolkitFragment);
        }
        if (updateFragment != null) {
            transaction.hide(updateFragment);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
